package grow_task

import (
	"go_server/game_server/constants"
	"go_server/game_server/dao/task_info_dao"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/game_server/world_manager"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
	"go_server/zinx/znet"
)

type GetGrowTaskListApi struct {
	znet.BaseRouter
}

// 显示成长、支线任务列表
func (*GetGrowTaskListApi) Handle(request ziface.IRequest) {
	// 解析请求消息
	msg := &pb.GetGrowTaskListC2S{}
	if !util.PBParseRet(request, msg) {
		return
	}

	// 获取连接对象和角色ID
	conn := request.GetConnection()
	roleId := ServiceObj.GetRoleId(request)

	// 获取成长、支线任务配置列表
	baseTaskList := config_manager.GetStcTableTask_SheetTaskListByTwoType(constants.GROW_TASK, constants.BRANCH_TASK)
	if len(baseTaskList) == 0 {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_GROW_BRANCH_TASK_UNFIND, nil)
		return
	}

	fields := []string{"grow_task_id", "grow_task", "branch_task"} // 需要查询或者修改的字段
	row := task_info_dao.GetTaskInfoByRoleId(roleId, fields)       // 获取当前用户任务进度

	var growTaskId int32                          // 当日活跃度
	growTaskProgress := make(map[int32][]int32)   // 成长任务进度
	branchTaskProgress := make(map[int32][]int32) // 支线任务进度

	if row != nil {
		growTaskId = row.GrowTaskId

		if row.GrowTask.Data() != nil {
			growTaskProgress = row.GrowTask.Data()
		}

		if row.BranchTask.Data() != nil {
			branchTaskProgress = row.BranchTask.Data()
		}
	}

	if growTaskId <= 0 {
		growTaskId = firstGrowTaskId
	}

	var growTaskInfo []int32 // 正在进行中的成长任务详情

	taskInfo, exist := growTaskProgress[growTaskId]
	if exist {
		growTaskInfo = append(growTaskInfo, taskInfo[0], taskInfo[1], taskInfo[2])
	} else {
		growTaskInfo = append(growTaskInfo, growTaskId, 0, 0)
	}

	branchTaskList := make(map[int32]*pb.BranchTaskInfo)

	// 遍历支线任务
	for key, value := range baseTaskList {
		if value.Type == constants.GROW_TASK {
			continue
		}

		// 已领取不返还
		branchTaskInfo, ok := branchTaskProgress[key]
		if ok && branchTaskInfo[2] == 2 {
			continue
		}

		// 有前置任务配置
		if value.PrereQuisites != nil {
			preTaskId := value.PrereQuisites[0] // 前置任务ID

			//判断当前前置任务是否已完成, 未完成则不返还
			preBranchTaskInfo, flag := branchTaskProgress[preTaskId]
			if !flag || preBranchTaskInfo[2] < 2 {
				continue
			}
		}

		branchTaskStruct := &pb.BranchTaskInfo{}

		if ok {
			branchTaskStruct.TaskId = branchTaskInfo[0]
			branchTaskStruct.Process = branchTaskInfo[1]
			branchTaskStruct.State = branchTaskInfo[2]
		} else {
			branchTaskStruct.TaskId = key
			branchTaskStruct.Process = 0
			branchTaskStruct.State = 0
		}

		branchTaskList[key] = branchTaskStruct
	}

	// 组装返回数据结构
	res := &pb.GetGrowTaskListS2C{
		GrowTaskInfo:   growTaskInfo,
		BranchTaskList: branchTaskList,
	}

	// 发送返回消息
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetGrowTaskList), res)
}

// 显示每日任务列表 下行推送
func (*GetGrowTaskListApi) GetGrowTaskListApiS2C(roleId int64) *pb.GetGrowTaskListS2C {
	// 获取成长、支线任务配置列表
	baseTaskList := config_manager.GetStcTableTask_SheetTaskListByTwoType(constants.GROW_TASK, constants.BRANCH_TASK)
	if len(baseTaskList) == 0 {
		zlog.Errorf("baseTaskList 成长或支线任务配置不存在")
		return nil
	}

	fields := []string{"grow_task_id", "grow_task", "branch_task"} // 需要查询或者修改的字段
	row := task_info_dao.GetTaskInfoByRoleId(roleId, fields)       // 获取当前用户任务进度

	var growTaskId int32                          // 当日活跃度
	growTaskProgress := make(map[int32][]int32)   // 成长任务进度
	branchTaskProgress := make(map[int32][]int32) // 支线任务进度

	if row != nil {
		growTaskId = row.GrowTaskId

		if row.GrowTask.Data() != nil {
			growTaskProgress = row.GrowTask.Data()
		}

		if row.BranchTask.Data() != nil {
			branchTaskProgress = row.BranchTask.Data()
		}
	}

	if growTaskId <= 0 {
		growTaskId = firstGrowTaskId
	}

	var growTaskInfo []int32 // 正在进行中的成长任务详情

	taskInfo, exist := growTaskProgress[growTaskId]
	if exist {
		growTaskInfo = append(growTaskInfo, taskInfo[0], taskInfo[1], taskInfo[2])
	} else {
		growTaskInfo = append(growTaskInfo, growTaskId, 0, 0)
	}

	branchTaskList := make(map[int32]*pb.BranchTaskInfo)

	// 遍历支线任务
	for key, value := range baseTaskList {
		if value.Type == constants.GROW_TASK {
			continue
		}

		// 已领取不返还
		branchTaskInfo, ok := branchTaskProgress[key]
		if ok && branchTaskInfo[2] == 2 {
			continue
		}

		// 有前置任务配置
		if value.PrereQuisites != nil {
			preTaskId := value.PrereQuisites[0] // 前置任务ID

			//判断当前前置任务是否已完成, 未完成则不返还
			preBranchTaskInfo, flag := branchTaskProgress[preTaskId]
			if !flag || preBranchTaskInfo[2] < 2 {
				continue
			}
		}

		branchTaskStruct := &pb.BranchTaskInfo{}

		if ok {
			branchTaskStruct.TaskId = branchTaskInfo[0]
			branchTaskStruct.Process = branchTaskInfo[1]
			branchTaskStruct.State = branchTaskInfo[2]
		} else {
			branchTaskStruct.TaskId = key
			branchTaskStruct.Process = 0
			branchTaskStruct.State = 0
		}

		branchTaskList[key] = branchTaskStruct
	}

	// 组装返回数据结构
	res := &pb.GetGrowTaskListS2C{
		GrowTaskInfo:   growTaskInfo,
		BranchTaskList: branchTaskList,
	}

	return res
}
